﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Evoucher.Business.Models;
using Mani.Common;

public partial class admin_vouchers_Package_AddEdit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (!String.IsNullOrEmpty(Request.QueryString["id"]))
                LoadPackage();
        }
    }

    private void LoadPackage()
    {
        IVoucherRepository repo = new VoucherRepository();
        int id = Convert.ToInt32(Request.QueryString["id"]);
        Package p = repo.GetPackageById(id);

        if (p == null)
            return;

        txtPackageName.Text = p.PackageName;
        cbActive.Checked = p.Active;
        txtPrice.Text = p.Price.ToString();
        txtStartDate.Text = p.StartDate.ToJalali();
        txtExpireDate.Text = p.ExpireDate.ToJalali();
        txtDescription.Text = p.Description;
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (String.IsNullOrEmpty(Request.QueryString["id"]))
            NewPackage();
        else
            UpdatePackage();

        UIHelper.ClientAlert(this, "تغییرات با موفقیت ذخیره شد");
        UIHelper.ClientRedirect(this, "packages.aspx");
    }

    private void NewPackage()
    {
        IVoucherRepository repo = new VoucherRepository();
        Package p = new Package();
        
        p.PackageName = txtPackageName.Text;
        p.Active = cbActive.Checked;
        p.Price = Convert.ToDouble(txtPrice.Text);
        p.StartDate = txtStartDate.Text.ToDateTime();
        p.ExpireDate = txtExpireDate.Text.ToDateTime();
        p.Description = txtDescription.Text;
        p.CreateDate = DateTime.Now;

        if (fuImage.PostedFile.InputStream.Length != 0)
        {
            byte[] image = new byte[fuImage.PostedFile.InputStream.Length];
            fuImage.PostedFile.InputStream.Read(image, 0, (int)(fuImage.PostedFile.InputStream.Length - 1));
            p.PackageImage = image;
        }

        repo.AddToPackages(p);
        repo.persist();
    }

    private void UpdatePackage()
    {
        IVoucherRepository repo = new VoucherRepository();
        int id = Convert.ToInt32(Request.QueryString["id"]);
        Package p = repo.GetPackageById(id);

        if (p == null)
            return;

        p.PackageName = txtPackageName.Text;
        p.Active = cbActive.Checked;
        p.Price = Convert.ToDouble(txtPrice.Text);
        p.StartDate = txtStartDate.Text.ToDateTime();
        p.ExpireDate = txtExpireDate.Text.ToDateTime();
        p.Description = txtDescription.Text;

        if (fuImage.PostedFile.InputStream.Length != 0)
        {
            byte[] image = new byte[fuImage.PostedFile.InputStream.Length];
            fuImage.PostedFile.InputStream.Read(image, 0, (int)(fuImage.PostedFile.InputStream.Length - 1));
            p.PackageImage = image;
        }

        repo.persist();
    }
}